package com.wisemoresystem.seal.action.mac.over;

import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.model.t_mac_move_log;
import com.wisemoresystem.seal.service.MacMoveLogService;
import com.wisemoresystem.seal.service.MacService;

/**
 * 上缴
 * 
 * @author 冯晓东
 *
 */
@Controller("macOverVerify")
@Scope("prototype")
@RequestMapping("/ix/mac/over/verify")
public class MacOverVerifyAction extends ProjBaseAction {

	@Autowired
	private MacService macService;
	@Autowired
	private MacMoveLogService macLogService;

	/**
	 * 上缴接收列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> macList = macService.selectOverApplyedList(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/mac/over/verify/list");
		mv.addObject("macList", macList);
		mv.addObject("srh_name", srh_name);
		return mv;
	}

	/**
	 * 去向检查页面
	 * 
	 * @param mac_id
	 *            印控仪id
	 * @return
	 */
	@RequestMapping("/verify_form")
	public ModelAndView verify_form(String mac_id) {

		t_mac mac = oc.findObjById(mac_id, t_mac.class);
		t_bank bank = oc.findObjById(mac.getBank_id(), t_bank.class);
		t_bank superBank = oc.findObjById(bank.getSuper_id(), t_bank.class);
		t_mac_move_log log = macLogService.findOverVerifyingLog(mac.get_id());

		ModelAndView mv = new ModelAndView("/jsp/mac/over/verify/verify_form");
		mv.addObject("mac", mac);
		mv.addObject("bank", bank);
		mv.addObject("superBank", superBank);
		mv.addObject("log", log);
		return mv;
	}

	/**
	 * 印控仪器上缴确认
	 * 
	 * @param mac_id
	 *            印控仪id
	 * @param verify_flag
	 *            检查状态
	 * @param note
	 * @return
	 */
	@RequestMapping("/verify")
	public ModelAndView verify(String mac_id, String verify_flag, String note) {

		boolean isStart = macLogService.over_verify(mac_id, verify_flag, note);
		if (isStart) {
			this.setTipMsg("上缴确认成功!", Tip.Type.success);
			logT.logH("印控仪上缴确认", t_mac.class, mac_id);
		} else {
			this.setTipMsg("上缴确认失败!该设备状态有误!", Tip.Type.error);
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 批量接收
	 * 
	 * @param check_ids
	 *            检查信息 所有id
	 * @param verify_flag
	 *            接收状态
	 * @param note
	 *            接收备注
	 * @return
	 */
	@RequestMapping("/verify_all")
	public ModelAndView verify_all(String check_ids, String verify_flag,
			String note) {

		String[] ids = check_ids.split(",");
		if ("yes".equals(verify_flag)) {
			verify_flag = "通过";
		} else {
			verify_flag = "不通过";
		}
		for (String mac_id : ids) {
			if (ChkTools.isNotNull(mac_id)) {

				boolean isStart = macLogService.over_verify(mac_id,
						verify_flag, note);
				if (isStart) {
					logT.logH("印控仪上缴确认", t_mac.class, mac_id);
				}
			}
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

}
